<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
       <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="名称搜索" field="name">
            <a-input v-model="searchForm.name" placeholder="请输入名称" allow-clear />
          </a-form-item>
          
        </a-col>
        <a-col :span="8">
          <a-form-item label="AppID搜索" field="app_id">
            <a-input v-model="searchForm.app_id" placeholder="请输入AppID" allow-clear />
          </a-form-item>
        </a-col>
      </template>
      <template #type="{ record }">
        {{record.type == 1 ? '公众号' : '小程序'}}
      </template>
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus( record)"></sa-switch>
      </template>
      <template #operationAfterExtend="{ record }">
        <!-- <a-link type="primary" @click="edit(record)"><icon-edit />编辑</a-link> -->
        <a-popconfirm
          content="确定要删除该数据吗?"
          position="bottom"
          @ok="deleteAction(record)">
          <a-link type="primary" v-auth="options.delete.auth || []">
            <icon-delete /> 删除
          </a-link>
        </a-popconfirm>
      </template>
      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '@/api/configuration'

// 引用定义
const crudRef = ref()
const editRef = ref()
const optionData = reactive({})

// 搜索表单
const searchForm = ref({
  protocol_type: '',
  title: '',
})

// SaTable 基础配置
const options = reactive({
  api: api.WechatAuthList,
  rowSelection: { showCheckedAll: true },
  showTools: false,
  add: {
    show: true,
    func: async () => {
      editRef.value?.open('add')
    },
  },
  edit: {
    show: true,
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: false,
  },
})

// SaTable 列配置
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 80 },
  { title: '名称', dataIndex: 'name', width: 180 },
  { title: '类型', dataIndex: 'type',  width: 180 },
  { title: 'AppID', dataIndex: 'app_id', width: 180 },
  { title: 'AppSecret', dataIndex: 'app_secret', width: 220 },
  { title: '是否默认', dataIndex: 'is_default', type: 'dict', dict: 'yes_or_no', width: 180 },
  { title: '状态', dataIndex: 'status', width: 180 },
  { title: '创建时间', dataIndex: 'created_at', width: 180 },
])

const deleteAction = async (record) => {
  const resp = await api.deleteWechatAuth({config_id: record.id})
  if (resp.code === 200) {
    Message.success(`删除成功！`)
    crudRef.value?.refresh()
  }
}

// 修改状态
const changeStatus = async (record) => {
  const response = await api.updateWechatAuth({ config_id: record.id, state:record.status==1?2:1 ,filed:'status'})
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}


// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
